#include "Camera2D.h"
#include "Log.h"
namespace SCION_RENDERING
{
  Camera2D::Camera2D()
      : Camera2D(640, 480)
  {
  }

  Camera2D::Camera2D(int width, int height)
      : m_Width{width}, m_Height{height}, m_Scale{1.f},
        m_Position{glm::vec2{0.f}}, m_CameraMatrix{1.f},
        m_OrthoProjection{1.f}, m_bNeedsUpdate{true}
  {
    m_OrthoProjection = glm::ortho(
        0.f,                          // 左
        static_cast<float>(m_Width),  // 右
        static_cast<float>(m_Height), // 下 (注意：这个应该是底部，在OpenGL中通常是0)
        0.f,                          // 上 (注意：这个应该是顶部，在OpenGL中通常是高度值)
        -1.f,                         // 近
        1.f                           // 远
    );
    update(); // 初始化时更新矩阵
  }
  void Camera2D::update()
  {
    if (!m_bNeedsUpdate)
    {
      return;
    }

    glm::mat4 viewMatrix = glm::mat4(1.f);
    viewMatrix = glm::translate(viewMatrix, glm::vec3(-m_Position, 0.f));
    viewMatrix = glm::scale(viewMatrix, glm::vec3(m_Scale, m_Scale, 1.f));

    m_CameraMatrix = m_OrthoProjection * viewMatrix;
    m_bNeedsUpdate = false;

    SCION_INFO("Camera Scale: " + std::to_string(m_Scale));
    SCION_INFO("Camera Position: (" + std::to_string(m_Position.x) + ", " + std::to_string(m_Position.y) + ")");
  }
}